package com.lmx.api.until;

import ch.qos.logback.core.net.server.Client;
import cn.hutool.core.lang.ClassScanner;
import cn.hutool.core.lang.Filter;
import com.apiclient.annotion.ApiClient;
import com.apiclient.annotion.MethodClient;
import com.apiclient.pojo.LmApiClient;
import com.apiclient.pojo.User;
import com.google.gson.Gson;
import net.bytebuddy.implementation.bytecode.assign.primitive.PrimitiveUnboxingDelegate;
import org.apache.ibatis.annotations.Param;

import java.lang.annotation.Annotation;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.util.Arrays;
import java.util.Set;

/**
 * 根据注解调用不同的方法
 */

public class ClientInvokeUntil {


    public static Object invokeByhostAndApiAndMethod(String accessKey, String secretKey, String host, String api, String method, String args) {
//        获取某个下所有类的反射
        Set<Class<?>> classes = ClassScanner.scanAllPackage("com.apiclient", new Filter<Class<?>>() {
            @Override
            public boolean accept(Class<?> aClass) {
                ApiClient annotation = (ApiClient) aClass.getAnnotation(ApiClient.class);
                if (annotation != null && annotation.interfanceHost().equals(host)) {
                    return true;
                }
                return false;
//                if (annotation)
            }
        });
//        获取该类下所有的方法
        for (Class cla : classes) {
            Method[] methods = cla.getMethods();
            for (Method methodClass : methods) {
                MethodClient methodClient = (MethodClient) methodClass.getAnnotation(MethodClient.class);
                if (methodClient != null) {
                    if (methodClient.api().equals(api) && methodClient.method().equals(method)) {
                        try {
                            Constructor constructor = cla.getConstructor(String.class, String.class);
                            Object o = constructor.newInstance(accessKey, secretKey);
                            Class<?>[] parameterTypes = methodClass.getParameterTypes();
                            System.out.println(parameterTypes[0]);
//                            parameterTypes[0].cast(args);
                            Gson gson = new Gson();
                            Object sss = gson.fromJson(args, parameterTypes[0]);
                            Object invoke = methodClass.invoke(o, sss);
                            System.out.println(methodClass.getName());
                            return invoke;
                        } catch (IllegalAccessException e) {
                            e.printStackTrace();
                        } catch (InvocationTargetException e) {
                            e.printStackTrace();
                        } catch (NoSuchMethodException e) {
                            e.printStackTrace();
                        } catch (InstantiationException e) {
                            e.printStackTrace();
                        }
                    }
                }

            }
        }
//        System.out.println(classes);
        return null;
    }


    public static void main(String[] args) {
//        ClientInvokeUntil clientInvokeUntil = new ClientInvokeUntil();
//        User user = new User();
//        user.setName("李满祥");
//        Gson gson = new Gson();
//        String s = gson.toJson("LMX");
//
//        Object post = ClientInvokeUntil.invokeByhostAndApiAndMethod("lmx", "abcdefg", "http://127.0.0.1:8123", "/name", "GET", "lmx");
//        System.out.println(post);

        LmApiClient lmApiClient = new LmApiClient("lmx","abcdefg");
        lmApiClient.getNameByPost("lmx");

//        LmApiClient apiClient = new LmApiClient("lmx","abcdefg");
//        System.out.println(apiClient.getNameByGet("user"));
    }

}

